package com.woniu.communitymanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.communitymanagement.entity.Building;
import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.entity.vo.BuildingVO;
import com.woniu.communitymanagement.service.BuildingService;
import com.woniu.communitymanagement.mapper.BuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author QIQI
* @description 针对表【building(楼宇表)】的数据库操作Service实现
* @createDate 2022-11-28 19:11:05
*/
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building>
    implements BuildingService{
    @Autowired(required = false)
    private BuildingMapper buildingMapper;
    @Override
    public HttpResult insertBuilding(BuildingVO building) {
        Building building1=new Building();
        building1.setId(null);
        building1.setNumbers(building.getNumbers());
        building1.setUints(building.getUints());
        building1.setRemarks(building.getRemarks());
        HttpResult httpResult=null;
        int count = buildingMapper.insert(building1);
        if (count>0){
            httpResult=new HttpResult(null,null,200,"添加成功");
        }else {
            httpResult=new HttpResult(null,null,500,"添加失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult deleteById(Integer id) {
        HttpResult httpResult=null;
        boolean isDelete = removeById(id);
        if(isDelete){
            httpResult=new HttpResult(null,null,200,"删除成功");
        }else {
            httpResult=new HttpResult(null,null,500,"删除失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult updateBuildingById(Building building) {
        HttpResult httpResult=null;
        boolean isUpdate = updateById(building);
        if (isUpdate){
            httpResult=new HttpResult(null,null,200,"修改成功");
        }else {
            httpResult=new HttpResult(null,null,500,"修改失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectAll(String numbers,Integer pageIndex,Integer pageSize) {
        HttpResult httpResult=null;
        String s=null;
        if( numbers!="null" && numbers!=null && numbers!=""){
            Pattern p=Pattern.compile("[^0-9]+");
            Matcher matcher = p.matcher(numbers);
            s = matcher.replaceAll("%");
        }else{
            s=null;
        }
        List<Building> buildings = buildingMapper.selectBuildingList(s, (pageIndex - 1) * pageSize, pageSize);
        Integer count = buildingMapper.selectCount(s);
        if(buildings!=null&&buildings.size()>0){
            httpResult=new HttpResult(buildings,count,200,"查询成功");
        }else {
            if(pageIndex>1){
                buildings = buildingMapper.selectBuildingList(s, (pageIndex - 2) * pageSize, pageSize);
                httpResult=new HttpResult(buildings,count,200,"查询成功");
            }else {
                httpResult=new HttpResult(null,count,500,"查询失败");
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult selectBuildingByid(Integer id) {
        HttpResult httpResult=null;
        Building building = buildingMapper.selectById(id);
        if (building!=null){
            httpResult=new HttpResult(building,1,200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult listAll() {
        HttpResult httpResult=null;
        List<Building> buildings = buildingMapper.selectList(null);
        if(buildings.size()>0){
            httpResult=new HttpResult(buildings,buildings.size(),200,"查询成功");
        }else {
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }
}




